/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.customControllers;

import com.bookshop.controllers.BookController;
import com.bookshop.entity.Book;
import com.bookshop.facade.BookFacade;
import com.bookshop.service.BookService;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Thibault
 */
@ManagedBean(name = "myBookController")
@SessionScoped
public class myBookController {
    
    @EJB
    private BookService service;
    
    @EJB
    private BookFacade bookFacade;
    private List<Book> books = new ArrayList<Book>();
    private Book book = new Book();
    private int catID = -1; 
    private String isbn;
   

    public List<Book> getBooks() {
        if (books.isEmpty()) {
            //books = bookFacade.findAll();
            books = service.getAllBooks();
        }
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    /**
     * Creates a new instance of myBookController
     */
    public myBookController() {
    }

    public void getAllBooks() {
        this.books.clear();
        //this.books = bookFacade.findAll();
        this.books = service.getAllBooks();
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
        myNav.handleNavigation(facesContext, null, "/index.xhtml");
    }
    
    public void getBookFromISBN(ActionEvent ae) {
        this.isbn = ae.getComponent().getAttributes().get("ISBN").toString();
        try {
            //this.book = bookFacade.getBookByID(isbn);
            this.book = service.getBookByISBN(this.isbn);
            
            FacesContext facesContext = FacesContext.getCurrentInstance();
            NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
	    myNav.handleNavigation(facesContext, null, "/book.xhtml");
            
        } catch (Exception ex) {
            Logger.getLogger(myBookController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void getBooksFromCategory(ActionEvent ae) {
        this.catID = Integer.parseInt(ae.getComponent().getAttributes().get("catID").toString());
        
        try {
            this.books.clear();
            //this.books = bookFacade.findByCategory(this.catID);
            this.books = service.getAllBooksByCategoryId(catID);
            
            FacesContext facesContext = FacesContext.getCurrentInstance();
            NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
	    myNav.handleNavigation(facesContext, null, "/index.xhtml");
            
        } catch (Exception ex) {
            Logger.getLogger(myBookController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
